package com.mijie.homi.search.service.index;

import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TaskService {

	@Autowired
	private TopicIndexService topicIndexService;
	@Autowired
	private UserIndexService userIndexService;
	@Autowired
	private MoodUserIndexService moodUserIndexService;
	
	private Timer topicIndexTaskWorker = new Timer();
	private Timer userIndexTaskWorker = new Timer();
	private Timer moodUserIndexTaskWorker = new Timer();
	
	private static final int ONE_MINUTE = 1000 * 60;
	private static final int TEN_MINUTE = ONE_MINUTE * 10;
	private static final int HALF_HOUR = TEN_MINUTE * 3 ;
	@PostConstruct
	private void initTask(){
		TimerTask topicIndexTask = new TimerTask(){
			public void run(){
				Calendar cal = Calendar.getInstance();
				//凌晨1点到5点不进行全量索引
				if(cal.get(Calendar.HOUR_OF_DAY)>=1 && cal.get(Calendar.HOUR_OF_DAY)<5)
					return ;
				topicIndexService.reIndex();
			}
		};
		topicIndexTaskWorker.schedule(topicIndexTask, ONE_MINUTE, HALF_HOUR);
		
		TimerTask userIndexTask = new TimerTask(){
			public void run(){
				Calendar cal = Calendar.getInstance();
				if(cal.get(Calendar.HOUR_OF_DAY)>=1 && cal.get(Calendar.HOUR_OF_DAY)<5)
					return ;
				userIndexService.reIndex();
			}
		};
		userIndexTaskWorker.schedule(userIndexTask, ONE_MINUTE , TEN_MINUTE * 2);
		
		TimerTask moodUserIndexTask = new TimerTask(){
			public void run(){
				Calendar cal = Calendar.getInstance();
				if(cal.get(Calendar.HOUR_OF_DAY)>=1 && cal.get(Calendar.HOUR_OF_DAY)<5)
					return ;
				moodUserIndexService.reIndex();
			}
		};
		moodUserIndexTaskWorker.schedule(moodUserIndexTask, ONE_MINUTE , TEN_MINUTE * 2);
		
	}
}
